package com.smart.speaker.websocket.tool;

import com.alibaba.dashscope.tools.FunctionDefinition;
import com.alibaba.dashscope.tools.ToolFunction;
import com.fasterxml.jackson.databind.JsonNode;
import com.google.gson.JsonObject;
import com.smart.speaker.util.JsonUtils;
import com.smart.speaker.util.StrUtils;
import org.springframework.stereotype.Component;

@Component("get_current_weather")
public class GetWeatherTool implements Tool {

    private final ToolFunction function;

    public GetWeatherTool() {
        JsonObject parameters = new JsonObject();
        JsonObject properties = new JsonObject();
        JsonObject location = new JsonObject();
        location.addProperty("type", "string");
        properties.add("location", location);
        parameters.addProperty("type", "object");
        parameters.add("properties", properties);
        function = ToolFunction.builder().function(FunctionDefinition.builder().name("get_current_weather")
                .description("获取指定地区的天气")
                .parameters(parameters).build()).build();
    }

    @Override
    public ToolResp call(String params) {
        String locationName;
        if (StrUtils.isEmpty(params)) {
            locationName = "杭州";
        } else {
            JsonNode jsonNode = JsonUtils.parseJsonToJsonNode(params);
            locationName = jsonNode.get("location").asText();
        }
        return new ToolResp(Type.RESPONSE, locationName + "的天气晴");
    }

    @Override
    public ToolFunction getFunction() {
        return function;
    }
}
